﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace JiuMeiMall.PMethod
{
    public static class Encrypt
    {
        //默认密钥向量 
        private static byte[] _key1 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        //密钥
        private static byte[] key = { 230, 204, 144, 184, 120, 185, 72, 195, 94, 146, 176, 3, 199, 146, 196, 108 };
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string MD5_Encrypt(string value)
        {
            byte[] result = Encoding.Default.GetBytes(value);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "").ToLower();
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="toDecrypt"></param>
        /// <returns></returns>
        public static string Decrypt(byte[] toDecrypt)
        {
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = key;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toDecrypt, 0, toDecrypt.Length);
            return Encoding.UTF8.GetString(resultArray);
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="str">原串</param>
        /// <returns></returns>
        public static byte[] EncryptStr(string str)
        {
            if (str.Length > 0)
            {
                byte[] bs = Encoding.UTF8.GetBytes(str);
                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = key;
                rDel.Mode = CipherMode.ECB;
                rDel.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = rDel.CreateEncryptor();
                return cTransform.TransformFinalBlock(bs, 0, bs.Length);
            }
            return null;
        }

        public static string Base64Decode(string Message)
        {
            if ((Message.Length % 4) != 0)
            {
                throw new ArgumentException("不是正确的BASE64编码，请检查。", "Message");
            }
            if (!System.Text.RegularExpressions.Regex.IsMatch(Message, "^[A-Z0-9/+=]*$", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
            {
                throw new ArgumentException("包含不正确的BASE64编码，请检查。", "Message");
            }
            string Base64Code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
            int page = Message.Length / 4;
            System.Collections.ArrayList outMessage = new System.Collections.ArrayList(page * 3);
            char[] message = Message.ToCharArray();
            for (int i = 0; i < page; i++)
            {
                byte[] instr = new byte[4];
                instr[0] = (byte)Base64Code.IndexOf(message[i * 4]);
                instr[1] = (byte)Base64Code.IndexOf(message[i * 4 + 1]);
                instr[2] = (byte)Base64Code.IndexOf(message[i * 4 + 2]);
                instr[3] = (byte)Base64Code.IndexOf(message[i * 4 + 3]);
                byte[] outstr = new byte[3];
                outstr[0] = (byte)((instr[0] << 2) ^ ((instr[1] & 0x30) >> 4));
                if (instr[2] != 64)
                {
                    outstr[1] = (byte)((instr[1] << 4) ^ ((instr[2] & 0x3c) >> 2));
                }
                else
                {
                    outstr[2] = 0;
                }
                if (instr[3] != 64)
                {
                    outstr[2] = (byte)((instr[2] << 6) ^ instr[3]);
                }
                else
                {
                    outstr[2] = 0;
                }
                outMessage.Add(outstr[0]);
                if (outstr[1] != 0)
                    outMessage.Add(outstr[1]);
                if (outstr[2] != 0)
                    outMessage.Add(outstr[2]);
            }
            byte[] outbyte = (byte[])outMessage.ToArray(Type.GetType("System.Byte"));
            return System.Text.Encoding.Default.GetString(outbyte);
        }

        /// <summary>
        /// 十六进制转换为二进制
        /// </summary>
        /// <param name="hexStr"></param>
        /// <returns></returns>
        public static byte[] parseHexStr2Byte(String hexStr)
        {
            if (hexStr.Length < 1)
                return null;
            byte[] result = new byte[hexStr.Length / 2];
            for (int i = 0; i < hexStr.Length / 2; i++)
            {
                int high = int.Parse(hexStr.Substring(i * 2, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                int low = int.Parse(hexStr.Substring(i * 2 + 1, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                result[i] = (byte)(high * 16 + low);
            }
            return result;
        }

        /// <summary>
        /// 字节转换为16进制字符串
        /// </summary>
        /// <param name="hexStr"></param>
        /// <returns></returns>
        public static string parseByte2HexStr(byte[] bs)
        {
            if (bs != null)
            {
                StringBuilder sb = new StringBuilder();
                foreach(byte b in bs)
                {
                    sb.Append(b.ToString("X2"));
                }
                return sb.ToString();
            }
            return null;
        }

    }
}
